package com.cpi.cornpp.purchase.program.converter;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.cpi.cornpp.purchase.program.dto.PurchaseOrderChargeDTO;
import com.cpi.cornpp.purchase.program.view.PurchaseOrderChargeView;

public final class PurchaseOrderChargeConverter {
	public static PurchaseOrderChargeView convert(PurchaseOrderChargeDTO dto){
		PurchaseOrderChargeView view = null;
		
		if (dto != null){
			view = new PurchaseOrderChargeView();
			
			view.setPurchaseOrderChargeTypeId((dto.getChargeTypeId() != null) ? dto.getChargeTypeId().toString() : null);
			view.setPurchaseOrderChargeTypeCode(dto.getChargeTypeCode());
			view.setPurchaseOrderId((dto.getPurchaseOrderId() != null) ? dto.getPurchaseOrderId().toString() : null);
			view.setPurchaseOrderAmount((dto.getAmount() != null) ? dto.getAmount().doubleValue() : 0.0D);
		}
		
		return view;
	}
	
	public static PurchaseOrderChargeDTO convert(PurchaseOrderChargeView view){
		PurchaseOrderChargeDTO dto = null;
		
		if (view != null){
			dto = new PurchaseOrderChargeDTO();
			
			dto.setChargeTypeId(StringUtils.isNotEmpty(view.getPurchaseOrderChargeTypeId()) ? Integer.valueOf(view.getPurchaseOrderChargeTypeId()) : null);
			dto.setPurchaseOrderId(StringUtils.isNotEmpty(view.getPurchaseOrderId()) ? Integer.valueOf(view.getPurchaseOrderId()) : null);
			dto.setAmount(new Double(view.getPurchaseOrderAmount()));
		}
		
		return dto;
	}
	
	public static List convertDTOList(List dtoList){
		PurchaseOrderChargeDTO dto = null;
		List viewList = null;
		
		viewList = new ArrayList(0);
		
		if (CollectionUtils.isNotEmpty(dtoList)){
			
			for (Iterator itr = dtoList.iterator(); itr.hasNext(); ){
				dto = (PurchaseOrderChargeDTO) itr.next();
				viewList.add(convert(dto));
			}
		}
		
		return viewList;
	}
	
	public static List convertViewList(List viewList){
		PurchaseOrderChargeView view = null;
		List dtoList = null;
		
		dtoList = new ArrayList(0);
		
		if (CollectionUtils.isNotEmpty(viewList)){
			
			for (Iterator itr = viewList.iterator(); itr.hasNext(); ){
				view = (PurchaseOrderChargeView) itr.next();
				dtoList.add(convert(view));
			}
		}
		
		return dtoList;
	}
}